from django.db import models
from django.contrib.auth.models import User
from rest_framework.authtoken.models import Token
from django.db.models.signals import post_save
from django.dispatch import receiver
from easy_thumbnails.fields import ThumbnailerImageField

"""
A signal to generate an authentication token once a user has been created.
""" 
@receiver(post_save, sender=User)
def create_auth_token(sender, instance=None, created=False, **kwargs):
    if created:
        Token.objects.create(user=instance)

class Excursion(models.Model):
    owner = models.ForeignKey('auth.User')
    name = models.CharField(max_length=256)
    password = models.CharField(max_length=16, blank=True)
    description = models.TextField(null=True)
    public = models.BooleanField(default=False, blank=True)
    date = models.DateField(null=True)
    time = models.TimeField(null=True, blank=True)

class Outcrop(models.Model):
    owner = models.ForeignKey('auth.User')
    name = models.CharField(max_length=254, unique=True)
    description = models.TextField(null=True)
    longitude = models.FloatField(null=True)
    latitude = models.FloatField(null=True)
    
    def __unicode__(self):
        return self.name

"""
A collection model, it contains all of the outcrops associated with an excursion
""" 
class Outcrop_Collection(models.Model):
    excursion = models.ForeignKey(Excursion)
    outcrop = models.ForeignKey(Outcrop)
    public = models.BooleanField(default=False)

class Image(models.Model):
    owner = models.ForeignKey('auth.User')
    excursion = models.ForeignKey(Excursion)
    outcrop = models.ForeignKey(Outcrop, null=True)
    name = models.CharField(max_length=256)
    image = ThumbnailerImageField(upload_to="userdocs/", null=True, blank=True)
    notes = models.TextField(null=True)
    public = models.BooleanField(default=False)
    altitude = models.FloatField(null=True)
    longitude = models.FloatField(null=True)
    latitude = models.FloatField(null=True)
    heading = models.FloatField(null=True)
    hAccuracy = models.FloatField(null=True)
    vAccuracy = models.FloatField(null=True)
    
class Tag(models.Model):
    image = models.ForeignKey(Image)
    tag = models.CharField(max_length=128)

"""
A collection model, it contains all of the users associated with an excursion. This association
represents that the user is a meber of that excursion.
""" 
class Excursion_Members(models.Model):
    user = models.ForeignKey('auth.User')
    excursion = models.ForeignKey(Excursion)
